import { useMutation, useQuery } from "react-query";
import { IPage, IPageParams } from "../../types";
import { useRequest } from "../../utils/request";
// 引入CryptoJS
import CryptoJS from "crypto-js";
const d = "7uYhf14";
const e = "h094";
const f = "&feki";

const g = "8AYhf";
const h = "14h094E";
const i = "S4jk";
// 十六位十六进制数作为秘钥
const key = CryptoJS.enc.Utf8.parse(d + e + f);
// 十六位十六进制数作为秘钥偏移量
const iv = CryptoJS.enc.Utf8.parse(g + h + i);

// aes加密方法
export function encrypt(data: string) {
  if (!data) {
    return "";
  }
  const srcs = CryptoJS.enc.Utf8.parse(data);
  const encrypted = CryptoJS.AES.encrypt(srcs, key, {
    mode: CryptoJS.mode.CBC,
    padding: CryptoJS.pad.Pkcs7,
    iv,
  });
  return encrypted.toString();
}
// aes解密方法
export function decrypt(data: string) {
  if (!data) {
    return "";
  }
  const decrypt = CryptoJS.AES.decrypt(data, key, {
    mode: CryptoJS.mode.CBC,
    padding: CryptoJS.pad.Pkcs7,
    iv,
  });
  const decryptedStr = decrypt.toString(CryptoJS.enc.Utf8);
  return decryptedStr;
}

/**
 * !获取工作台统计接口
 * @returns
 */
export const useGetUserBaseStatitic = () => {
  const request = useRequest<IWokrStatistic>();
  return useQuery(["getUserBaseStatistic"], () =>
    request("/beef/getUserBaseStatistic", { data: {} }).then((res) => {
      res.todoBeefs.forEach((li) => {
        li.phone = decrypt(li.phone);
        li.certificateNumber = decrypt(li.certificateNumber);
      });
      return res;
    })
  );
};

/**
 * !获取矛盾纠纷接口
 * @returns
 */
export const useGetBeefBySearch = (queryParams: IBeefSearchParams) => {
  const request = useRequest<IPage<IBeef>>();
  return useQuery(["getBeefBySearch", queryParams], () =>
    request("/beef/getBeefBySearch", { data: queryParams }).then((res) => {
      res.list.forEach((li) => {
        li.phone = decrypt(li.phone);
        li.certificateNumber = decrypt(li.certificateNumber);
      });
      return res;
    })
  );
};
/**
 * !获取矛盾类型
 * @returns
 */
export const useGetAllBeefTypeMap = () => {
  const request = useRequest<any>();
  return useQuery(["getAllBeefTypeMap"], () =>
    request("/beef/getAllBeefTypeMap", {data:{} }).then((res) => {
      return Object.values(res);
    })
  );
};
/**
 * !根据id查询当事人接口
 * @returns
 */
export const useGetById = () => {
  const request = useRequest<string>();
  return useMutation(async (params: { id: string }) => {
    const result = request("/litigantInfo/getById", {
      data: params,
      method: "GET",
    });
    return result;
  });
};
/**
 * !根据矛盾数据id查询当事人接口
 * @returns
 */
export const useGetByBeefId = () => {
  const request = useRequest<string>();
  return useMutation(async (params: { beefId: string | undefined }) => {
    const result = request("/litigantInfo/getByBeefId", {
      data: params,
      method: "GET",
    });
    return result;
  });
};
/**
 * ! 添加矛盾纠纷接口
 * @returns
 */
export const useAddBeef = () => {
  const request = useRequest<string>();
  return useMutation(async (params: IAddBeef) => {
    params.phone = encrypt(params.phone);
    params.certificateNumber = encrypt(params.certificateNumber);
    const result = request("/beef/addBeef", {
      data: params,
      method: "POST",
      json: true,
    });
    return result;
  });
};

/**
 * ! 添加当事人信息接口
 * @returns
 */
export const useAddDsrBeef = () => {
  const request = useRequest<string>();
  return useMutation(async (params: IAddDsrUser) => {
    const result = request("/litigantInfo/add", {
      data: params,
      json: true,
      method: "POST",
    });
    return result;
  });
};
/**
 * ! 修改当事人信息接口
 * @returns
 */
export const useUpdateDsrBeef = () => {
  const request = useRequest<string>();
  return useMutation(async (params: IAddDsrUser) => {
    const result = request("/litigantInfo/update", {
      data: params,
      json: true,
      method: "POST",
    });
    return result;
  });
};
/**
 * !根据id删除当事人接口
 * @returns
 */
export const useRemoveById = () => {
  const request = useRequest<string>();
  return useMutation(async (params: { id: string }) => {
    const result = request("/litigantInfo/removeById", {
      data: params,
      method: "POST",
    });
    return result;
  });
};
/**
 * ! 修改矛盾纠纷接口
 * @returns
 */
export const useUpdateBeefById = () => {
  const request = useRequest<string>();
  return useMutation(async (params: IAddBeef) => {
    params.phone = encrypt(params.phone);
    params.certificateNumber = encrypt(params.certificateNumber);
    const result = request("/beef/updateBeefById", {
      data: params,
      method: "POST",
      json:true
    });
    return result;
  });
};



/**
 * !删除矛盾纠纷接口
 */
export const useDeletedBeefById = () => {
  const request = useRequest<string>();
  return useMutation(async (params: { beefId: string }) => {
    const result = request("/beef/deletedBeefById", {
      data: params,
      method: "DELETE",
    });
    return result;
  });
};

export type IWokrStatistic = {
  /**
   * 统计数量
   */
  all: number;

  /**
   * 待处理
   */
  todo: number;

  /**
   * 待处理的列表
   */
  todoBeefs: IBeef[];
};

/**
 * 修改矛盾纠纷
 */
export type IUpdateBeef = {
  beefId: string;
} & Partial<IAddBeef>;

/**
 * 添加矛盾纠纷
 */
export type IAddBeef = {
  //事件名称
  beefName:string,
  //归属网络
  belongNetwork:string
  //风险等级
  riskLevel:string
  //归属网络
  beefTime:string,
  //涉及单位
  involveUnit:string,
  //涉及人数
  involveCount:string,
  //涉湖类型
  involveType:string,
  //事件规模
  eventScale:string,
  //当事人id集合
  litigantIdList:Array<number>

  /**
   * 申请人姓名
   */
  xm: string;

  /**
   * 申请人电话
   */
  phone: string;

  /**
   * 证件号码
   */
  certificateNumber: string;

  /**
   * 纠纷类型
   */
  beefType: number;

  /**
   *地址
   */
  beefAddress: string;

  /**
   * 纠纷详情
   */
  beefDetail: string;

  /**
   * 图片地址数组
   */
  imgUrls: string[];

  /**
   * 派出所ID
   */
  unitId: string;

  /**
   * 纠纷来源; 1=走访 2=矛排;;
   */
  beefSource: number;
  /**
   * 性别
   */
  userSex: string;

  /**
   * 社区ID
   */
  sqUnitId: string;

  /**
   * 是否租户; 1=是;2=否;
   */
  isZu: number;

  /**
   * 房东姓名
   */
  houseUserName: string;

  /**
   * 房东电话号码
   */
  houseUserPhone: string;

  /**
   * 租房开始时间
   */

  startTime: string;

  /**
   * 租房结束时间
   */
  endTime: string;

  /**
   * 企业/学校/居民
   */
  zfType: string;

  /**
   * 经度
   */
  longitude: string;

  /**
   * 维度
   */
  latitude: string;

  /**
   * 走访图片
   */
  zfImages: string;
};
// 添加当事人信息
export type IAddDsrUser = {
  id:any,
  litigantName:string,
  category:string,
  gender:string,
  nation:string,
  phone:string,
  cardType:string,
  cardNumber:string,
  academicDegree:string,
  belongingArea:string,
  roadName:string,
  communityName:string,
  buildingRoom:string,
  enterpriseCreditCode:string,
  keyConcerns:string,
  threatsMade:string,
  createTime:string,

}
/**
 * 搜索参数
 */
export type IBeefSearchParams = {
  beefName: string;
  xm: string;
  phone: string;
  certificateNumber: string;
  beefType: number | null;
  status: number | null;
  unitId: string;
  startTime: string;
  endTime: string;
  beefSource: number | null;
  userName: string;
} & IPageParams;

/**
 * 矛盾纠纷类型
 */
export type IBeef = {
  /**
   * 矛盾id
   */
  beefLitigants:[];
  beefName:string;
  beefId: string;
  belongNetwork:string;
  beefTime:string;
  involveUnit:string;
  involveCount:string;
  involveType:string;
  eventScale:string;
  /**
   * 被走访人姓名
   */
  xm: string;

  /**
   * 被走访人电话
   */
  phone: string;

  /**
   * 被走访人证件类型
   */
  certificateType: string;
  /**
   * 证件号码
   */
  certificateNumber: string;

  /**
   * 性别
   */
  userSex: string;

  /**
   * 纠纷类型
   */
  beefType: number;

  /**
   * 地址
   */
  beefAddress: string;

  /**
   * 纠纷详情
   */
  beefDetail: string;

  /**
   * 流程状态; 1=申请;2签收;3=指派人员处理;4=调解; 5=确认;6=电话回访; 7=退回流程;8=取消;100=综治中心
   */
  status: number;

  /**
   * 创建时间
   */
  createTime: string;

  /**
   * 更新时间
   */
  updateTime: string;

  /**
   * 纠纷来源; 1=走访; 2=矛排;
   */
  beefSource: number;

  /**
   * 申请人微信ID
   */
  wxId: string;

  /**
   * 当前流程用户ID
   */
  beefFlowUserId: string;

  /**
   * 单位ID
   */
  unitId: string;
  /**
   * 图片地址数组
   */
  imgUrls: string[];

  /**
   *  风险等级; 0=待评估;1=一般类;2=关注类;3=重点
   */
  riskLevel: string;

  /**
   * 社区ID
   */
  sqUnitId: string;

  /**
   * 是否租户; 1=是;2=否;
   */
  isZu: number;

  /**
   * 房东姓名
   */
  houseUserName: string;

  /**
   * 房东电话号码
   */
  houseUserPhone: string;

  /**
   * 租房开始时间
   */

  startTime: string;

  /**
   * 租房结束时间
   */
  endTime: string;

  /**
   * 企业/学校
   */
  zfType: string;

  /**
   * 经度
   */
  longitude: string;

  /**
   * 维度
   */
  latitude: string;

  /**
   * 走访图片
   */
  zfImages: string;
};
